<template>
	<view class="myFavorite">
        <SRV-topbar title="我的收藏" :isLeft="true"></SRV-topbar>
		<view class="nav" v-if="false">
			<view :class="index==navindex?'item check':'item'" v-for="(item,index) in nav" :key="index" @click="bindEditTabbar(index)">{{item}}</view>
		</view>
		<view class="con">
			<view class="goods" v-for="(item,index) in pageResult.list" :key="index" @click="openView(index)">
				<image v-if="item.status != 1 ? true : false" class="hasBeen" src="../../static/img/hasBeen.png" mode=""></image>
				
				<image v-if="item.topic_type == 'product'" :src="item.picture"  mode="aspectFill"></image>
				<view class="right" v-if="item.topic_type == 'product'">
					<view class="right-title" ><text class="combo">{{item.reductionName}}</text>{{item.name}}</view>
					<view class="num">
						<view>￥{{item.minPrice + (item.minPrice != item.maxPrice ? ' - ' + item.maxPrice : '')}}</view>
						<view class="oldpic" v-if="item.marketMinPrice > 0">￥{{item.marketMinPrice + (item.marketMinPrice != item.marketMaxPrice ? ' - ' + item.marketMaxPrice : '')}}</view>
					</view>
					<image class="del" src="https://hdsq.aoorange.cn/attachment/xcx/icon/del.png" mode="aspectFill" @tap.stop="delCollect($event, index)"></image>
				</view>
				
				
				<image v-if="item.topic_type == 'merchant'" :src="item.picture" mode="aspectFill" @click="jump('store_view', item.topic_id ,item.status)"></image>
				<view v-if="item.topic_type == 'merchant'" class="right">
					<text @click="jump('store_view', item.topic_id ,item.status)">{{item.name}}</text>
					
					<!-- <view class="num" @click="jump('store_view', item.topic_id)">
						<text>{{item.description}}</text>
					</view> -->
					<view class="right_con" @click="jump('store_view', item.topic_id, item.status)">
						<uni-rate class="uni-rate-dis" disabled="true" :value="item.desc_credit" size="16"></uni-rate>
						<view class="right_de">
							<text class="rate">{{item.cate}}</text>
							<text class="km">{{ item.distance }}KM</text>
						</view>
						
					</view>
					<image class="del" src="http://app.chinasrv.com/attachment/xcx//icon/del.png" mode="aspectFill" @click="delCollect(index)"></image>
				</view>
			</view>
		</view>
		<view v-if="pageResult.list.length == 0" class="nocontent">
			<image src="https://hdsq.aoorange.cn/attachment/xcx/icon/noItem.png"></image>
			<text>您暂未收藏商品～</text>
		</view>


	</view>
</template>

<script>
    var _self;
	import serve from '../../static/request.js';
	import storage from '../../static/appkey.js';
	import common from '../../static/jump.js';
	export default {
		data() {
			return {
				nav: ["商品", "商户"],
				list:[{},{}],
				navindex:0,
				pageParam: {
					appkey: uni.getStorageSync('appkey'),
					'access-token': uni.getStorageSync('access-token'),
					type: 'product',
					page: 0,
					pageSize: 10,
					latitude: uni.getStorageSync('latitude'),
					longitude:uni.getStorageSync('longitude'),
				},
				pageResult: {
					list: [],
					isEmpty: false,
					isEnd: false
				},
			}
		},
		methods: {
			bindEditTabbar(index) {
				if (index == _self.navindex) {
					return ;
				}
				_self.navindex = index;
				// 商品和商店切换
				_self.pageParam.type = _self.navindex == 0 ? 'product' : 'merchant';
				_self.pageParam.page = 0;
				_self.pageResult.list = [];
				_self.loadList();
			},
			loadList() {
				_self.pageParam.page++
				serve.request({
                    url: '/member/collect/list',
                    data: _self.pageParam,
                    v: '/v2'
                }).then(res => {
                    if (res.data && res.data.length > 0) {
                        _self.pageResult.list = _self.pageResult.list.concat(res.data);
                    }
                    _self.pageResult.isEmpty = _self.pageResult.list.length == 0;
                    _self.pageResult.isEnd = res.data.length == 0;
                });
			},
			delCollect(e, key) {
				e.preventDefault();
				let collectId = _self.pageResult.list[key].id;
				uni.showModal({
					title: '提示',
					content: '您确定删除这条收藏吗',
					success: function(res) {
						if (res.confirm) {
							serve.request({
                                url: '/member/collect/del',
                                data: {
                                    appkey: uni.getStorageSync('appkey'),
                                    'access-token': uni.getStorageSync('access-token'),
                                    id: collectId,
                                },
                                v: '/v2'
                            }).then(res => {
                                uni.showToast({
                                    title: res.message,
                                });
                                if (res.code == 200) {
                                    _self.pageResult.list.splice(key, 1);
                                }
                            });
						}
					}
				});
			},
            openView(index) {
                let item = _self.pageResult.list[index];
                if(item.status != 1) {
                    uni.showToast({
                        title: '商品已下架',
                        icon: 'none'
                    });
                    return ;
                }
                common.jump(item.jumpType, item.jumpLink);    
            },
			jump(jump_type, jump_link, status) {
                common.jump(jump_type, jump_link);			
			}
		},
		onLoad() {
            _self = this;
			_self.loadList();
		},
		onPullDownRefresh() {
			_self.pageParam.page = 0;
			_self.pageResult.list = [];
			_self.loadList();
		},
		//触底加载
		onReachBottom() {
			if (_self.pageResult.isEnd || _self.pageResult.isEmpty) {
				return
			}
			_self.loadList();
		}
	}
</script>

<style scoped lang="scss">
	.myFavorite {
		.nav {
			display: flex;
			align-items: center;
			height: 100rpx;
			border-bottom: 1rpx rgba(204, 204, 204, 0.3) solid;
			background: #FFFFFF;
			position: fixed;
			width: 100%;
			z-index: 10;

			.item {
				font-size: 32rpx;
				color: rgba(153, 153, 153, 1);
				line-height: 100rpx;
				width: 50%;
				text-align: center;
			}

			.check {
				position: relative;
				color: #232323;
			}

			.check::after {
				content: "";
				position: absolute;
				width: 30rpx;
				height: 7rpx;
				background: rgba(255, 188, 0, 1);
				border-radius: 3rpx;
				bottom: 0;
				left: 46%;
			}
		}

		.con {
			// padding-top: 100rpx;
		}

		.goods {
			box-sizing: border-box;
			display: flex;
			align-items: center;
			padding: 40rpx 30rpx;
			border-bottom: 1rpx rgba(235, 236, 240, 1) solid;
			position: relative;
			.hasBeen{
				width: 180rpx;
				height: 180rpx;
				position: absolute;
				right: 20rpx;
				top: 40rpx;
				z-index: 1;
				opacity: 0.5;
			}
			image {
				width: 170rpx;
				height: 170rpx;
				margin-right: 20rpx;
			}

			.right {
				position: relative;
				width: 503rpx;
				height: 170rpx;
				display: flex;
				// justify-content: space-between;
				flex-direction: column;
				font-size: 32rpx;
				z-index: 2;

				.right-title {
					color: #333333;
					font-weight: bold;
					padding-right: 28rpx;
					-webkit-line-clamp: 2;
					overflow: hidden;
					word-break: break-all;
					text-overflow: ellipsis; 
					display: -webkit-box; // 弹性伸缩盒
					-webkit-box-orient: vertical; // 设置伸缩盒子元素排列方式
				}
                .combo {
                    background: rgba(235, 89, 56, 1);
                    padding: 0 8rpx;
                    color: #fff;
                }
				.del {
					position: absolute;
					width: 32rpx;
					height: 31rpx;
					right: -30rpx;
				}

				.num {
					font-size: 28rpx;
					color: #FF6364;
					font-weight: bold;

					.oldpic {
						font-weight: 400;
						color: #CCCCCC;
						font-size: 24rpx;
						text-decoration: line-through
					}
				}
				.right_con {
					display: flex;
					flex-direction: column;
					align-items: flex-start;
					position: relative;
					margin-top: 40rpx;
					.uni-rate-dis{
						display: block;
					}
				
					.name {
						max-width: 300rpx;
						font-size: 36rpx;
					}
				
					.state {
						padding: 5rpx 8rpx;
						text-align: center;
						color: #FFFFFF;
						font-size: 20rpx;
						border-radius: 2rpx;
						margin-left: 10rpx;
					}
					.right_de{
						display: flex;
						align-items: center;
						justify-content: space-around;
						margin-top: 30rpx;
						
						.rate {
							color: #FFBC00;
							font-size: 32rpx;
							margin-left: 10rpx;
							margin-top: -9rpx;
						}
										
						.km {
							color: #999999;
							position: absolute;
							right: 0rpx;
							font-size: 32rpx;
						}
					}
					
				
					.discounts {
						font-size: 20rpx;
						color: #EB5938;
						margin-left: 7rpx;
					}
				}
			}
		}

		.nocontent {
			width: 100%;
			padding-top: 338rpx;
			display: flex;
			flex-direction: column;
			align-items: center;

			image {
				width: 220rpx;
				height: 220rpx;
			}

			text {
				font-size: 28rpx;
				color: rgba(204, 204, 204, 1);
				margin-top: 30rpx;
			}
		}
	}
</style>
